package ar.edu.unq.wannaplay.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;

import ar.edu.unq.wannaplay.exceptions.IncorrectPasswordException;
import ar.edu.unq.wannaplay.exceptions.UserDoesNotExistException;
import ar.edu.unq.wannaplay.pages.Home;
import ar.edu.unq.wannaplay.roles.AbstractLinkAdderStrategy;
import ar.edu.unq.wannaplay.services.UserService;
import ar.edu.unq.wannaplay.users.Role;

public class LoginPanel extends ServicedPanel<UserService> {

    private static final long serialVersionUID = 1L;

    private String username;

    private String password;

    private FeedBackPanel feedBackPanel;

    public LoginPanel(final String id, final UserService service, final FeedBackPanel feedBackPanel) {
        super(id, service);
        this.feedBackPanel = feedBackPanel;
        this.initialize();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void initialize() {
        CompoundPropertyModel loginModel = new CompoundPropertyModel(this);
        Form form = new Form("form", loginModel);
        form.add(new TextField("username"));
        form.add(new PasswordTextField("password"));
        form.add(this.createLoginButton("login"));
        this.add(form);

    }

    private AjaxButton createLoginButton(final String id) {
        return new AjaxButton(id) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
                try {
                    LoginPanel.this.getService().login(LoginPanel.this.getUsername(), LoginPanel.this.getPassword());
                    Role role = LoginPanel.this.getService().getUserRole(LoginPanel.this.getUsername());
                    AbstractLinkAdderStrategy.setUser(LoginPanel.this.getService(), role);
                    this.setResponsePage(Home.class);
                } catch (UserDoesNotExistException e) {
                    LoginPanel.this.getFeedBackPanel().changeMessage(
                            this.getLocalizer().getString("login.userDoesNotExist", this));
                    target.addComponent(LoginPanel.this.getFeedBackPanel());
                } catch (IncorrectPasswordException e) {
                    LoginPanel.this.getFeedBackPanel().changeMessage(
                            this.getLocalizer().getString("login.incorrectPassword", this));
                    target.addComponent(LoginPanel.this.getFeedBackPanel());
                }
            }
        };
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(final String username) {
        this.username = username;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public FeedBackPanel getFeedBackPanel() {
        return feedBackPanel;
    }

    public void setFeedBackPanel(final FeedBackPanel panel) {
        feedBackPanel = panel;
    }
}
